package com.three.productList.service.impl;

import com.three.core.entity.ProductOrder;
import com.three.core.entity.UserIntegral;
import com.three.core.vo.R;
import com.three.productList.dao.ProductOrderDao;
import com.three.productList.feign.FrontUserVipFeign;
import com.three.productList.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author ：li
 * @date ：Created in 2025/3/18 018 18:50
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private FrontUserVipFeign frontUserVipFeign;

    @Override
    public R<ProductOrder> insertProductOrder(ProductOrder productOrder) {
        //判断积分够不够
        R<UserIntegral> userIntegralR = frontUserVipFeign.selectIntegralByUserId(productOrder.getUserId());
        if (userIntegralR.getData().getCredits()<productOrder.getTotalIntegral().intValue()){
            return new R<>(400,"积分不足",null);
        }
        //创建订单减积分（锁单)，前端支付时做判断
        //userIntegralR.getData().setCredits(userIntegralR.getData().getCredits()-productOrder.getTotalIntegral().intValue());
        //productOrderDao.updateById()

        //是否只启用积分，不消耗余额
        if(productOrder.getTotalAmount() == null){
            productOrder.setTotalAmount(BigDecimal.valueOf(0.00));
        }
        productOrder.setUpdateTime(LocalDateTime.now());
        int row = productOrderDao.insert(productOrder);
        if (row == 0) {
            return new R<>(400, "订单提交失败", null);
        }
        return new R<>(200, "订单提交成功", null);
    }
}
